package com.tjtt.tdm.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.PagingBean;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.Organization;
import com.tjtt.tdm.system.dao.OrganizationDao;
import com.tjtt.tdm.system.service.OrganizationService;

public class OrganizationServiceImpl implements OrganizationService {
	private static final Log log = LogFactory.getLog(OrganizationServiceImpl.class);
	private OrganizationDao organizationDao;


	public OrganizationDao getOrganizationDao() {
		return organizationDao;
	}



	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

	public List<Organization> getAllTypes() throws ServiceException {
		try {
			return organizationDao.getAllTypes();
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public List getAllTypesBFR() throws ServiceException {
		try {
			return organizationDao.getAllTypesBFR();
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	public List getAllTypesB() throws ServiceException {
		try {
			return organizationDao.getAllTypesB();
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	public List getAllTypesF() throws ServiceException {
		try {
			return organizationDao.getAllTypesF();
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	public List getAllTypesR() throws ServiceException {
		try {
			return organizationDao.getAllTypesR();
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public PageResult findOrganizationByParentId(int orgParentId,
			Map<String, String> filterMap) {
		return this.organizationDao.findOrganizationByParentId(orgParentId,filterMap);
	}

	public PageResult queryOrganizationList(Map<String, String> filterMap) {
		return this.organizationDao.queryOrganizationList(filterMap);
	}

	public void saveOrganization(Organization organization) throws ServiceException {
		try {
			organizationDao.save(organization);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(),e);
		}
	}


	public List<Organization> findByProperty(String orgName) throws ServiceException {
		try {
			return organizationDao.findByProperty("orgName",orgName);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(),e);
		}
	}



	public void removeOrganization(String ids) throws ServiceException {
		String idss[] = ids.split(",");
		for(String id : idss){
			Organization organization = new Organization();
			organization.setOrgId(Long.parseLong(id));
			this.organizationDao.delete(organization);
		}
	}



	public void updateOrganization(Organization organization)
			throws ServiceException {
		try {
			organizationDao.update(organization);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(),e);
		}
		
	}

	public Organization findOrganizationById(Long id) {
		return this.organizationDao.getOrganizationById(id);
	}
	
	
	/**
	 * 根据条件获取组织结构的总量
	 * @param params
	 * @return
	 * @throws ServiceException 
	 */
	public Long getOrgCount(HashMap params) throws ServiceException{
		List<Organization> orgCount = null;
		try {
			orgCount = organizationDao.findByProperties(params);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
		Long count = new Long (orgCount.size());
		return count;
	}
 
	/**
	 * 查找用户表里的组织id
	 * @return
	 * @throws ServiceException String
	 * @author 张海娜
	 * @date 2012-10-22 下午03:11:40
	 */
	public PageResult findUserByOrgId(String ids, Map<String, String> filterMap)
			throws ServiceException {
		return this.organizationDao.findUserByOrgId(ids,filterMap);
	}

	/**
	 * 根据id获取组织信息
	 * @param id
	 * @return
	 * @throws ServiceException Organization
	 * @author 张海娜
	 * @date 2012-11-13 上午11:18:05
	 */
	public Organization getOrganizations(Long orgId) throws ServiceException {
		try {
			return organizationDao.get(orgId);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/**
	 * 保存组织信息
	 * @param organizations
	 * @throws ServiceException void
	 * @author 张海娜
	 * @date 2012-11-13 上午11:21:36
	 */
	public void saveOrganizations(Organization organizations)
			throws ServiceException {
		try {
			organizationDao.saveOrUpdate(organizations);//保存组织信息
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(), e);
		}
	}
	/**
	 * 根据删除标记delflag为1，查询机构名称是否存在
	 * @param orgName
	 * @return PageResult
	 * @author 张海娜
	 * @return 
	 * @date 2012-11-13 下午05:33:47
	 */
	public PageResult findOrganizationByDelflag(String orgName,Map<String, String> filterMap,Long orgParentId) {
		return organizationDao.findOrganizationByDelflag(orgName,filterMap,orgParentId);
	}

	public PageResult findOrgByDelflag(Long orgId,String orgName,Map<String, String> filterMap) {
		return organizationDao.findOrgByDelflag(orgId,orgName,filterMap);
	}

	public PageResult findUserById(int id, Map<String, String> filterMap) {
		return this.organizationDao.findUserById(id,filterMap);
	}



	public PageResult queryUserList(Map<String, String> filterMap) {
		return this.organizationDao.queryUserList(filterMap);
	}


	/**
	 * 查询名称是否存在
	 * @throws Exception void
	 * @author 张海娜
	 * @date 2013-2-21 下午02:06:16
	 */
	public Organization findOrganizationName(String orgName) {
		return this.organizationDao.findOrganizationName(orgName);
	}
	/**
	 * 查询编号是否存在
	 * @throws Exception void
	 * @author 张海娜
	 * @date 2013-2-21 下午02:06:16
	 */
	public Organization findOrganizationByorgNo(String orgNo) {
		return organizationDao.findOrganizationByorgNo(orgNo);
	}



	
	public PageResult findOrganizationorgNoByDelflag(String orgNo, Map filterMap,Long pId) {
		return organizationDao.findOrganizationorgNoByDelflag(orgNo,filterMap,pId);
	}



	
	public PageResult queryuserByOrganationid(Map<String, String> filterMap,
			String userByOrganationid) {
		return this.organizationDao.queryuserByOrganationid(userByOrganationid,filterMap);
	}



	
	public boolean isUsingItem(String ids, String[] tables, String[] fieldNames){
		return organizationDao.isUsingItem(ids, tables, fieldNames);
	}



	
	public List<Organization> getAllTypes4() throws ServiceException {
		try {
			return organizationDao.getAllTypes4();
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}




	public List<Organization> getAllTypesBuFen() throws ServiceException {
		try {
			return organizationDao.getAllTypesBuFen();
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
}
